﻿using Microsoft.EntityFrameworkCore.ChangeTracking;
using System.Linq.Expressions;

namespace EFAPIProject.Repositories
{
    public interface IRepository
    {
        public interface IRepository<TEntity> where TEntity : class
        {
            // 新增
            Task<EntityEntry<TEntity>> AddAsync(TEntity entity, CancellationToken cancellationToken = default);
            Task AddRangeAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);

            // 删除
            void Remove(TEntity entity);
            void RemoveRange(IEnumerable<TEntity> entities);

            // 更新
            void Update(TEntity entity);
            void UpdateRange(IEnumerable<TEntity> entities);

            // 查询
            Task<TEntity?> GetByIdAsync(object id, CancellationToken cancellationToken = default);
            Task<TEntity?> GetFirstAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
            Task<List<TEntity>> GetAllAsync(CancellationToken cancellationToken = default);
            Task<List<TEntity>> GetFilteredAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);

            // 辅助方法
            Task<bool> ExistsAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default);
            Task<int> CountAsync(Expression<Func<TEntity, bool>>? predicate = null, CancellationToken cancellationToken = default);

            // 保存更改
            Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
        }


    }
}
